package com.ls.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ls.controller.version.model.AddVersionModel;
import com.ls.controller.version.model.EditVersionModel;
import com.ls.controller.version.model.QueryVersionModel;
import com.ls.controller.version.model.UpdateStatusModel;
import com.ls.entity.Version;
import com.ls.mapper.VersionMapper;
import com.ls.service.IVersionService;
import com.ls.utils.ResultData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

import static com.ls.constant.SystemConstants.VALID_TRUE;

/**
 * <p>
 * 版本更新表 服务实现类
 * </p>
 *
 * @author pengyu
 * @since 2024-11-20
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class VersionServiceImpl extends ServiceImpl<VersionMapper, Version> implements IVersionService {
    /** 版本更新列表 */
    @Override
    public ResultData pageList(QueryVersionModel model) {
        LambdaQueryWrapper<Version> wrapper = new LambdaQueryWrapper<Version>()
                .orderByDesc(Version::getCreateTime);
        Page page = new Page(model.getPageNum(), model.getPageSize());
        IPage<Version> dtoIPage = this.baseMapper.selectPage(page, wrapper);
        return ResultData.successData(dtoIPage);
    }
    /**新增版本*/
    @Override
    public ResultData add(AddVersionModel model) {
        log.info("新增版本:{}", model);
        Version version1 = getOne(new LambdaQueryWrapper<Version>()
                .eq(Version::getCurrentVersion, model.getCurrentVersion()));
        if (version1 != null) {
            return ResultData.failure("当前版本已存在");
        }
        Version version=new Version();
        BeanUtils.copyProperties(model,version);
        version.setValidFlag(VALID_TRUE);
        version.setCreateTime(LocalDateTime.now());
        version.setUpdateTime(LocalDateTime.now());
        Boolean flag = saveOrUpdate(version);
        if (flag) {
            return ResultData.successData(version);
        } else {
            return ResultData.failure("新增失败");
        }
    }
    /**编辑版本*/
    @Override
    public ResultData edit(EditVersionModel model) {
        Version version1 = getOne(new LambdaQueryWrapper<Version>()
                .eq(Version::getCurrentVersion, model.getCurrentVersion())
                .ne(Version::getVersionId, model.getVersionId())
        );
        if (version1 != null) {
            return ResultData.failure("当前版本已存在");
        }
        Version version=getById(model.getVersionId());
        BeanUtils.copyProperties(model,version);
        version.setValidFlag(VALID_TRUE);
        version.setCreateTime(LocalDateTime.now());
        version.setUpdateTime(LocalDateTime.now());
        Boolean flag = saveOrUpdate(version);
        if (flag) {
            return ResultData.successData(version);
        } else {
            return ResultData.failure("编辑失败");
        }
    }
    /**删除版本*/
    @Override
    public ResultData del(Integer versionId) {
        Version version = getById(versionId);
        if (version == null){
            return ResultData.failure("版本不存在");
        }
        Boolean flag = version.deleteById();
        if (flag) {
            return ResultData.success();
        } else {
            return ResultData.failure("删除失败");
        }
    }
    /**修改版本启用状态*/
    @Override
    public ResultData updateStatus(UpdateStatusModel model) {
        Version version = getById(model.getVersionId());
        if (version == null){
            return ResultData.failure("版本不存在");
        }
        version.setUpdateTime(LocalDateTime.now());
        version.setValidFlag(model.getValidFlag());
        return updateById(version) ? ResultData.success() : ResultData.failure("修改失败");
    }
    /**获取当前最新版本*/
    @Override
    public ResultData getVersion() {
        Version version = getOne(new LambdaQueryWrapper<Version>()
                .eq(Version::getValidFlag, VALID_TRUE)
                .orderByDesc(Version::getCurrentVersion)
                .last("limit 1")
        );
        return ResultData.successData(version);
    }

}
